package cn.t.modules.scheduler.core.schedule;

import cn.t.modules.scheduler.common.enums.ScheduleStatus;
import cn.t.modules.scheduler.core.SqlProcessor;
import cn.t.modules.scheduler.core.schedule.repository.po.SchedulePo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 计划队列
 * @author 陶敏麒
 * @date 2024/3/6 21:41
 */
@Component
@Slf4j
public class ScheduleQueue extends LinkedBlockingQueue<SchedulePo> {

    @Resource
    private SqlProcessor sqliteProcessor;

    public ScheduleQueue() {
        super(2048);
    }

    public void offerSchedule(SchedulePo schedulePo) {
        schedulePo.setStatus(ScheduleStatus.RUNNING);
        if (!super.offer(schedulePo)) {
            this.sqliteProcessor.updateStatus(schedulePo.getId(), ScheduleStatus.ACCEPTED, "队列已满", schedulePo.getVersion());
        }
    }
}
